package com.xu.t1.middleware.threadallocation;

import com.xu.t1.model.ThreadAllocationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 应开线程数量责任链工厂类
 *
 * @author xu
 *
 */

@Component
public class ThreadAllocationChainFactory {

    @Autowired
    private ThreadHandler1 threadHandler1;

    @Autowired
    private ThreadHandler2 threadHandler2;

    @Autowired
    private ThreadHandler3 threadHandler3;

    @Autowired
    private ThreadHandler4 threadHandler4;

    public Integer createChain(int totalSize) {
        threadHandler1.setNext(threadHandler2);
        threadHandler2.setNext(threadHandler3);
        threadHandler3.setNext(threadHandler4);

        ThreadAllocationContext context = new ThreadAllocationContext();
        context.setTotalSize(totalSize);

        threadHandler1.handle(context);

        return context.getAllocatableThreads();
    }
}